﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SaleStat.COM.Constant;
using SaleStat.BLL;
using SaleStat.COM.Exceptions;

public partial class Login : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!Page.IsPostBack)
            {
                //焦点设置
                txtUserName.Focus();

            }
        }
        catch (Exception ex)
        {
            ExceptionHandler(ex);
        }

    }

    #region 登录按钮事件
    /// <summary>
    /// 登录按钮事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void imgBtnLogin_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            getUserInfo();
            f_Info(CommonConstant.OPERATIONS.LOGIN, "登录系统");
            //跳转到masterpage
            Response.Redirect("~/CM/CM001F001.aspx", false);
        }
        catch (Exception ex)
        {
            ExceptionHandler(ex);
           

        }
    }
    #endregion

    #region 获取用户信息
    /// <summary>
    /// 获取用户信息
    /// </summary>
    public void getUserInfo()
    {
        //获取用户名和密码
        string strUserName = txtUserName.Text.ToString();
        string strPassWord = txtPassword.Text.ToString();

        //调用bll层方法获取用户信息
        Login_BLL loginBllService = new Login_BLL();
        DataTable dtUserInfo = loginBllService.getUserInfo(strUserName, strPassWord);
        if (dtUserInfo == null || dtUserInfo.Rows.Count == 0)
        {
            //业务异常
            throw new BusinessException("用户名或密码错误！");
        }

        //session中设置值
        Session[SessionKeyConstant.SESSION_USERNAME] = strUserName;
        Session[SessionKeyConstant.SESSION_USERID] = dtUserInfo.Rows[0]["UserID"].ToString();
        Session[SessionKeyConstant.SESSION_PURVIEW] = dtUserInfo.Rows[0]["PermissionName"].ToString();
        string strIpAddress = Request.UserHostAddress;
        Session[SessionKeyConstant.SESSION_IPADDRESS] = strIpAddress;
    }
    #endregion
}
